package com.example.util;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.core.view.ViewCompat;
import androidx.recyclerview.widget.RecyclerView;

/**
 * @Description:
 * @Author: 葛浩杰
 * @CreateDate: 2021/6/24 17:50
 */
public class MyBehavior extends CoordinatorLayout.Behavior<View> {

    private float deltaY;

    public MyBehavior(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean layoutDependsOn(@NonNull CoordinatorLayout parent, @NonNull View child, @NonNull View dependency) {
        return dependency instanceof RecyclerView;
    }

    @Override
    public boolean onDependentViewChanged(@NonNull CoordinatorLayout parent, @NonNull View child, @NonNull View dependency) {

        if(deltaY == 0){
            deltaY = dependency.getY() - child.getHeight();
        }
        Log.e("ghj", dependency.getY() + "----" + dependency.getTranslationY());
        Log.e("ghj", child.getHeight() + "");

        float dy = dependency.getY() - child.getHeight();
        dy = dy < 0 ? 0 : dy;
        float y = -(dy/deltaY) * child.getHeight();
        child.setTranslationY(y);

        return true;
    }
}
